package com.spider.iceCream.spiderComponent.parse;

import com.google.gson.JsonArray;
import com.google.gson.JsonParseException;

import com.spider.iceCream.spiderComponent.model.replyModel;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import com.spider.iceCream.spiderComponent.util.HttpRequestUtil;

import java.util.ArrayList;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

//获取一个json文件的评论
public class GetReply {

    private Integer i=0;

    public List<replyModel> reply(String html) throws JsonParseException, Exception {
        List<replyModel> dataList = new ArrayList<replyModel>();
        JsonParser jp = new JsonParser();
        JsonObject jo = jp.parse(html).getAsJsonObject();

        JsonArray jsonArray = (JsonArray) jo.get("data").getAsJsonObject().get("replies");
        Iterator iterator = jsonArray.iterator();
        while (iterator.hasNext()) {
            JsonObject in = (JsonObject) iterator.next();
            String message = in.get("content").getAsJsonObject().get("message").getAsString();
            String uname = in.get("member").getAsJsonObject().get("uname").getAsString();
            Integer level = in.get("member").getAsJsonObject().get("level_info").getAsJsonObject().get("current_level").getAsInt();
            String sex = in.get("member").getAsJsonObject().get("sex").getAsString();
            Integer like = in.get("like").getAsInt();
            long ctime = in.get("ctime").getAsLong();
            Date date = new Date();
            date.setTime(ctime * 100);

            replyModel model = new replyModel();
            model.setUname(uname);
            model.setSex(sex);
            model.setLevel(level);
            model.setLike(like);
            model.setMessage(message);
            model.setData(date);

            dataList.add(model);
            this.i++;
//            System.out.println(this.i);
            // showData(model);
        }
        return dataList;

    }
    //根据oid获取视频所以评论
    public List<replyModel> allReply(Integer oid) throws Exception {
        JsonParser jp = new JsonParser();
        HttpRequestUtil httpRequestUtil = new HttpRequestUtil();
        List<replyModel> replyModels = new ArrayList<replyModel>();
        boolean is_end;         //判断是否到最后一个json文件
        int j = 1;              //json文件起始位置
        while (true) {
            String replyUrl = "https://api.bilibili.com/x/v2/reply/main?jsonp=jsonp&next=" + j + "&type=1&oid=" + oid + "&mode=3&plat=1";
            String replyHtml = httpRequestUtil.getContentByGetMethod(replyUrl, "utf-8");
            JsonObject jo = jp.parse(replyHtml).getAsJsonObject();
            is_end = jo.get("data").getAsJsonObject().get("cursor").getAsJsonObject().get("is_end").getAsBoolean();
            if (this.i>=500) {
                is_end = true;
            }
            if (is_end != false) {
                break;
            }
            replyModels.addAll(this.reply(replyHtml));
            j++;
            Thread.sleep(100);
        }
        //打印获取的所有评论
//        for (replyModel rm : replyModels) {
//            System.out.println(rm.toString());
//        }
        return replyModels;
    }

    public static void showData(replyModel model) {
        System.out.print(model.getUname() + "\t");
        System.out.print(model.getLevel() + "\t");
        System.out.println(model.getSex());
    }
}
